package com.kelvin.paopao.module.login.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AndroidException;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.airsaid.pickerviewlibrary.OptionsPickerView;
import com.kelvin.kelvinlib.utils.LogUtil;
import com.kelvin.kelvinlib.utils.ToastUtil;
import com.kelvin.paopao.R;
import com.kelvin.paopao.injector.components.DaggerLoginComponent;
import com.kelvin.paopao.injector.modules.LoginModule;
import com.kelvin.paopao.module.base.BaseActivity;
import com.kelvin.paopao.module.login.login.bindAccount.BindAccountActivity;
import com.kelvin.paopao.module.login.register.registerStep1.RegisteStep1Activity;
import com.kelvin.paopao.module.login.register.resetpwd.ResetPwdActivity;
import com.kelvin.paopao.utils.DialogMaker;
import com.kelvin.paopao.widgets.CustomToolbar;
import com.kelvin.paopao.widgets.dialog.OptionSelector;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;

/**
 * Created by Administrator on 2017/5/7.
 */

public class LoginActivity extends BaseActivity<ILoginPresenter> implements PlatformActionListener {
    @BindView(R.id.custom_toolbar)
    CustomToolbar customToolbar;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.tv_login)
    TextView tvLogin;

    private Platform platform;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_login;
    }

    @Override
    protected void initInjector() {
        DaggerLoginComponent.builder().applicationComponent(getAppComponent())
                .loginModule(new LoginModule(this)).build().inject(this);
    }

    @Override
    protected void initViews() {

        customToolbar.showBackIcon(this);

        etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String user_id = etPhone.getText().toString().trim();
                String pwd = etPassword.getText().toString().trim();
                if (!TextUtils.isEmpty(user_id) && !TextUtils.isEmpty(pwd)) {
                    tvLogin.setBackgroundResource(R.drawable.shape_login_btn);
                    tvLogin.setTextColor(ContextCompat.getColor(LoginActivity.this, android.R.color.white));
                } else {
                    tvLogin.setBackgroundResource(R.drawable.shape_login2_btn);
                    tvLogin.setTextColor(ContextCompat.getColor(LoginActivity.this, R.color.text_color_dark));
                }
            }
        });
        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String user_id = etPhone.getText().toString().trim();
                String pwd = etPassword.getText().toString().trim();
                if (!TextUtils.isEmpty(user_id) && !TextUtils.isEmpty(pwd)) {
                    tvLogin.setBackgroundResource(R.drawable.shape_login_btn);
                    tvLogin.setTextColor(ContextCompat.getColor(LoginActivity.this, android.R.color.white));
                } else {
                    tvLogin.setBackgroundResource(R.drawable.shape_login2_btn);
                    tvLogin.setTextColor(ContextCompat.getColor(LoginActivity.this, R.color.text_color_dark));
                }
            }
        });
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @OnClick({R.id.tv_login, R.id.tv_find_pwd, R.id.iv_wechat_login, R.id.iv_qq_login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_login:
                String user_id = etPhone.getText().toString().trim();
                String pwd = etPassword.getText().toString().trim();
                if (TextUtils.isEmpty(user_id)) {
                    ToastUtil.showToast(this, "手机号码不能为空");
                    return;
                }
                if (TextUtils.isEmpty(pwd)) {
                    ToastUtil.showToast(this, "密码不能为空");
                    return;
                }
                mPresenter.login(user_id, pwd, null);
                break;
            case R.id.tv_find_pwd:
                ResetPwdActivity.launch(this);
                break;
            case R.id.iv_wechat_login:
                loginByWECHAT();
                break;
            case R.id.iv_qq_login:
                ToastUtil.showToast(this, "即将开放");
                break;
        }
    }

    /**
     * 微信登录
     */
    private void loginByWECHAT() {

        platform = ShareSDK.getPlatform(this, Wechat.NAME);
        if (platform.isAuthValid()) {
            //已经授权
            platform.removeAccount(true);
        }

        DialogMaker.showProgressDialog(this, "登录中,请稍候~~");
        //使用SSO授权，通过客户单授权
        platform.SSOSetting(false);
        platform.setPlatformActionListener(this);
        platform.showUser(null);
    }


    public static void launch(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }

    private String id;

    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        LogUtil.d("wechat login success!");
        id = platform.getDb().getUserId();
        mPresenter.login(null, null, id);
    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        ToastUtil.showToast(this, "微信授权失败");
    }

    @Override
    public void onCancel(Platform platform, int i) {
        ToastUtil.showToast(this, "微信授权取消," + i);
    }

    public void showSelector() {
        OptionsPickerView selector = OptionSelector.showWechatSeletor(this);
        selector.setOnOptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int option1, int option2, int option3) {
                if (option1 == 0) {
                    //绑定账户
                    BindAccountActivity.launch(LoginActivity.this, id);
                } else {
                    //新增账户
                    RegisteStep1Activity.launch(LoginActivity.this, id);
                }
            }
        });
    }

}
